<?php

/*
function getMicrotime() {
  list($usec, $sec) = explode(" ",microtime());
  return ((float)$usec + (float)$sec);
}

$GLOBALS["t1"] = getMicrotime();
*/

require(dirname(__FILE__)."/../core/cms.php");
		
class aAdmin extends cCms {

	function __construct($siteDir, $dir, $dbUser, $dbName, $dbPass="", $dbHost="localhost")
	{
		parent::__construct($siteDir, $dir, $dbUser, $dbName, $dbPass, $dbHost);
		$this->defaultView = new cView(array("mode"=>"backend","group"=>"standard"), $this->defaultView);


		if (strToLower($_GET["style"])=="ajax")
		{
			// Anonymní useři nemají přístup do admina
			if ($this->user->anonymous) 
			{
				// TODO : vyresit, ajax je oteviran YUI komponentou v iframe. nejspis poslat hlavicku
				die("");
			}

			/*	Nastaví se view
			 * 
			 */			
			$this->view = new cView(array(),$this->defaultView);

			$this->view->recognize();

			$gClass = $this->file->loadGroup($this->view->group);
			$group = new $gClass($this);
			
			/*	Zpracuje se akce
			 * 
			 */
			if (isset($_POST["_action"])) {
				$group->handlePost($_POST, $this->view);
			} else if (isset($_GET["_action"])) {
				$_POST["_action"] = $_GET["_action"];
				$group->handlePost($_POST, $this->view);
			}
			
			header('Content-Type: text/html; charset=utf-8');
			echo html_entity_decode($group->handleRequest($this->view));
		} else $this->getWholePage();
	}

	function getWholePage()
	{		
		// Anonymní useři nemají přístup do admina
		if ($this->user->anonymous) 
		{
			die(include("loginlayout.php"));
		}

		$view = new cView($_GET, $this->defaultView);
		$gClass = $this->file->loadGroup($view->group);
		$group = new $gClass($this);
		$view = new cView($_GET, $group->defaultView);
		$view->override($_POST);

		$this->view = $view;

		if (isset($_POST["_action"])) {
			$group->handlePost($_POST, $view);
		}

		//return $group->getWholePage(); až budou hotovy layouty!	
		
		$tree = $group->getTree($view);
		$browser = html_entity_decode($group->getBrowser($view));
		$hotlinks = $group->getHotlinks($view);	

		$groups = $this->file->listGroups();
		foreach ($groups as $g=>$name) {
			$s.="<a href=\"?group=$g\" >$name</a> ";
		}
		$groups = $s;

		$url="".$view->getUrl(); 
		$defaultUrl="".$view->getUrl();
		
		header('Content-Type: text/html; charset=utf-8');
		include("layout.php");		
	}
	
}

?>